package com.hongning.railway.ui.main.fragments.dynamic;

import android.app.Application;
import android.arch.lifecycle.ViewModel;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableField;
import android.databinding.ObservableInt;
import android.databinding.ObservableList;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;

import com.hongning.railway.BR;
import com.hongning.railway.R;
import com.hongning.railway.app.Constant;
import com.hongning.railway.data.AppRepository;
import com.hongning.railway.entity.DynamicEntity;
import com.hongning.railway.entity.LoginEntity;
import com.hongning.railway.entity.reqbean.ReqAddViewOrPraise;
import com.hongning.railway.entity.reqbean.ReqDynamicBean;
import com.hongning.railway.entity.resbean.ResDynamicBean;
import com.hongning.railway.entity.resbean.ResNewsBean;
import com.hongning.railway.ui.main.fragments.dynamic.itemviewmodel.Type1ItemViewModel;
import com.hongning.railway.ui.main.fragments.dynamic.itemviewmodel.Type2ItemViewModel;
import com.hongning.railway.ui.main.fragments.home.HomeViewModel;
import com.hongning.railway.ui.main.fragments.home.itemviewmodel.NewItemViewModel;
import com.hongning.railway.util.Apputils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Observable;

import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.MultiItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.http.ApiDisposableObserver;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;
import me.tatarka.bindingcollectionadapter2.OnItemBind;

public class DynamicViewModel extends BaseViewModel<AppRepository> {
    private int page = 1;
    public UIChangeData uc = new UIChangeData();
    public LoginEntity loginEntity=Apputils.getLoginInfo();
    private String staffid;
    /**
     * 传给后台类型筛选id
     * 101001：活动相册
     *102001：党建动态
     */
    private String newsTypeIds="101001,102001";

    public int getPage() {
        return page;
    }

    public String getStaffid() {
        return staffid;
    }
    public void setVisiable(){
        if (visiable.get()==View.GONE){
            visiable.set(View.VISIBLE);
        }else {
            visiable.set(View.GONE);
        }
    }
    public class UIChangeData{
        //下拉刷新完成
        public SingleLiveEvent finishRefreshing = new SingleLiveEvent<>();
        //上拉加载完成
        public SingleLiveEvent finishLoadmore = new SingleLiveEvent<>();
        //是否已经到最后
        public SingleLiveEvent enableLoadmore = new SingleLiveEvent<>();

        public SingleLiveEvent showCategory = new SingleLiveEvent();
    }
    public ObservableInt visiable = new ObservableInt(View.GONE);
    public BindingCommand allCommond = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            newsTypeIds="101001,102001";
            page=1;
            staffid=null;
            setVisiable();
            getCategoryListData();
        }
    });
    public BindingCommand dynamicCommond = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            newsTypeIds="102001";
            page=1;
            staffid=null;
            setVisiable();
            getCategoryListData();
        }
    });

    public BindingCommand photoCommond = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            newsTypeIds="101001";
            page=1;
            staffid=null;
            setVisiable();
            getCategoryListData();
        }
    });
    public BindingCommand mydynamicCommond = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            newsTypeIds="102001";
            setVisiable();
            page=1;
            if (loginEntity.getUserinfo()!=null){
                staffid=loginEntity.getUserinfo().getStaffId();
            }
            getCategoryListData();
        }
    });
    public BindingCommand myphotoCommond = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            newsTypeIds="101001";
            page=1;
            setVisiable();
            if (loginEntity.getUserinfo()!=null){
                staffid=loginEntity.getUserinfo().getStaffId();
            }
            getCategoryListData();
        }
    });

    public BindingCommand onRefreshCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            page=1;
            getCategoryListData();
        }
    });


    public BindingCommand onLoadMoreCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            getCategoryListData();
        }
    });

    public DynamicViewModel(@NonNull Application application, AppRepository model) {
        super(application, model);
    }

    public ObservableList<MultiItemViewModel> viewmodelList = new ObservableArrayList<>();

    public ItemBinding<MultiItemViewModel> itemBinding = ItemBinding.of(new OnItemBind<MultiItemViewModel>() {
        @Override
        public void onItemBind(ItemBinding itemBinding, int position, MultiItemViewModel item) {
            Integer itemType = (Integer) item.getItemType();
            if (itemType==1){
                itemBinding.set(BR.viewModel, R.layout.dynamic_item_type1);
            }else {
                itemBinding.set(BR.viewModel, R.layout.dynamic_item_type2);
            }
        }
    });
    public void getCategoryListData(){
        ReqDynamicBean reqDynamicBean=new ReqDynamicBean();
        if (loginEntity==null)return;
        if (Apputils.getLoginInfo().getGroups()!=null&&!Apputils.getLoginInfo().getGroups().isEmpty()){
            String groupId =Apputils.getLoginInfo().getGroups().get(0).getGroupid();
            if (TextUtils.isEmpty(groupId)){
                reqDynamicBean.setOrgId(groupId);
            }
        }
        reqDynamicBean.setPageSize(Constant.PAGE_SIZE);
        reqDynamicBean.setPageNum(page);
        reqDynamicBean.setNewsTypeId(newsTypeIds);
        if (!TextUtils.isEmpty(staffid)){
            reqDynamicBean.setStaffId(staffid);
        }
        reqDynamicBean.setUserId(loginEntity.getUserinfo().getStaffId());
        model.getNewsDynamicList(reqDynamicBean)
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .compose(RxUtils.schedulersTransformer())
                .subscribe(new ApiDisposableObserver<ResDynamicBean>() {
                    @Override
                    public void onResult(ResDynamicBean resDynamicBeans) {
                        if (page==1){
                            uc.finishRefreshing.call();
                            viewmodelList.clear();
                        }else {
                            uc.finishLoadmore.call();
                        }
                        if (resDynamicBeans.getData().size()<Constant.PAGE_SIZE){
                            uc.enableLoadmore.call();
                        }
                        page++;
                        for (ResDynamicBean.DataBean data:resDynamicBeans.getData()) {
                            if (data.getType()==1){
                                viewmodelList.add(new Type1ItemViewModel(DynamicViewModel.this,data));
                            }else {
                                viewmodelList.add(new Type2ItemViewModel(DynamicViewModel.this,data));
                            }
                        }
                    }
                });
    }

    /**
     * 点赞
      */
    public void clickPraise(String contentId,String isLike){
        ReqAddViewOrPraise reqAddViewOrPraise = new ReqAddViewOrPraise();
        reqAddViewOrPraise.setUserId(Apputils.getUserId());
        reqAddViewOrPraise.setContentId(contentId);
        reqAddViewOrPraise.setIsLike(isLike);
        model.addvieworPraise(reqAddViewOrPraise)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .subscribe(new Consumer() {
                    @Override
                    public void accept(Object o) throws Exception {
                        //这种情况居然连基本的格式都没有了
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                });
    }
}
